package com.example.teamtribe.main.mine.info;

import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.PopupPics;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.SchoolStore;
import com.example.teamtribe.utils.Utility;
import com.jph.takephoto.model.TResult;

import java.io.File;

public class MyInfoActivity extends PhotoActivity implements View.OnClickListener, MyInfoContract.View, PopupPics.OnPickListener {

    private ImageView ivHead;
    private EditText etNickname;
    private EditText etSchool;
    private EditText etNo;
    private EditText etName;
    private EditText etGender;
    private EditText etBirthday;
    private EditText etAcademy;
    private EditText etMajor;
    private EditText etGrade;
    private EditText etClass;

    private SwitchCompat swiSchool;
    private SwitchCompat swiNo;
    private SwitchCompat swiName;
    private SwitchCompat swiGender;
    private SwitchCompat swiBirthday;
    private SwitchCompat swiAcademy;
    private SwitchCompat swiMajor;
    private SwitchCompat swiGrade;
    private SwitchCompat swiClass;

    private User user;
    private File head;
    private PopupPics popup;
    private MyInfoContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_info);

        ((TextView) findViewById(R.id.tv_title)).setText("我的信息");

        ivHead = findViewById(R.id.iv_head);
        etNickname = findViewById(R.id.et_nickname);
        etSchool = findViewById(R.id.et_school);
        etNo = findViewById(R.id.et_no);
        etName = findViewById(R.id.et_name);
        etGender = findViewById(R.id.et_gender);
        etBirthday = findViewById(R.id.et_birthday);
        etAcademy = findViewById(R.id.et_academy);
        etMajor = findViewById(R.id.et_major);
        etGrade = findViewById(R.id.et_grade);
        etClass = findViewById(R.id.et_cla);

        swiSchool = findViewById(R.id.switch_school);
        swiNo = findViewById(R.id.switch_no);
        swiName = findViewById(R.id.switch_name);
        swiGender = findViewById(R.id.switch_gender);
        swiBirthday = findViewById(R.id.switch_birthday);
        swiAcademy = findViewById(R.id.switch_academy);
        swiMajor = findViewById(R.id.switch_major);
        swiGrade = findViewById(R.id.switch_grade);
        swiClass = findViewById(R.id.switch_cla);

        user = Utility.getUser();
        popup = new PopupPics(this, this);
        presenter = new MyInfoPresenter(this);

        setInfo();
        setSwitches();

        ivHead.setOnClickListener(this);
        findViewById(R.id.fab_yes).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                popup.show(v);
                break;

            case R.id.fab_yes:
                presenter.update(head, etNickname.getText().toString().trim(), user.getPortrait(), getIsPublic());
                break;

            default:
                break;
        }
    }

    @Override
    public void next() {
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        String path;
        if (result.getImage().isCompressed()) {
            path = result.getImage().getCompressPath();
        } else {
            path = result.getImage().getOriginalPath();
        }
        head = new File(path);
        Glide.with(this).load(head).into(ivHead);
    }

    private void setInfo() {
        Glide.with(this).load(Utility.getUri(user.getPortrait())).into(ivHead);
        etNickname.setText(user.getNickname());

        if (user.getSchoolId() != 0) {
            etSchool.setText(SchoolStore.getSchoolNameById(user.getSchoolId()));
            etNo.setText(user.getNo());
            etName.setText(user.getName());
            etGender.setText(user.getGender() == 1 ? "男" : "女");
            etBirthday.setText(user.getBirthday());
            etAcademy.setText(user.getAcademy());
            etMajor.setText(user.getMajor());
            etGrade.setText(user.getGrade() + "");
            etClass.setText(user.getCla() + "班");
        }
    }

    private void setSwitches() {
        if (user.getSchoolId() == 0) {
            swiSchool.setEnabled(false);
            swiNo.setEnabled(false);
            swiName.setEnabled(false);
            swiGender.setEnabled(false);
            swiBirthday.setEnabled(false);
            swiAcademy.setEnabled(false);
            swiMajor.setEnabled(false);
            swiGrade.setEnabled(false);
            swiClass.setEnabled(false);
            return;
        }
        char[] isPublic = user.getIsPublicArray();
        if (isPublic[8] == '1') {
            swiSchool.setChecked(true);
        }
        if (isPublic[7] == '1') {
            swiNo.setChecked(true);
        }
        if (isPublic[6] == '1') {
            swiName.setChecked(true);
        }
        if (isPublic[5] == '1') {
            swiGender.setChecked(true);
        }
        if (isPublic[4] == '1') {
            swiBirthday.setChecked(true);
        }
        if (isPublic[3] == '1') {
            swiAcademy.setChecked(true);
        }
        if (isPublic[2] == '1') {
            swiMajor.setChecked(true);
        }
        if (isPublic[1] == '1') {
            swiGrade.setChecked(true);
        }
        if (isPublic[0] == '1') {
            swiClass.setChecked(true);
        }
    }

    private int getIsPublic() {
        StringBuilder builder = new StringBuilder("000000000");
        if (swiSchool.isChecked()) {
            builder.setCharAt(8, '1');
        }
        if (swiNo.isChecked()) {
            builder.setCharAt(7, '1');
        }
        if (swiName.isChecked()) {
            builder.setCharAt(6, '1');
        }
        if (swiGender.isChecked()) {
            builder.setCharAt(5, '1');
        }
        if (swiBirthday.isChecked()) {
            builder.setCharAt(4, '1');
        }
        if (swiAcademy.isChecked()) {
            builder.setCharAt(3, '1');
        }
        if (swiMajor.isChecked()) {
            builder.setCharAt(2, '1');
        }
        if (swiGrade.isChecked()) {
            builder.setCharAt(1, '1');
        }
        if (swiClass.isChecked()) {
            builder.setCharAt(0, '1');
        }
        return Integer.parseInt(builder.toString(), 2);
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), getCropOptions());
    }
}